package cn.byapps.luckincoffee.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import com.youth.banner.loader.ImageLoaderInterface;

import java.util.ArrayList;
import java.util.List;

import cn.byapps.luckincoffee.R;

public class HomeFragment extends Fragment {
    private Banner banner;
    private HomeViewModel homeViewModel;

    int[] imageResourceID = new int[]{R.drawable.swiper1, R.drawable.swiper2, R.drawable.swiper3};
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);

//        final TextView textView = root.findViewById(R.id.text_home);
//        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                textView.setText(s);
//            }
//        });

        banner = (Banner) root.findViewById(R.id.banner);
        List<Integer> imgeList = new ArrayList<>();
        for (int i = 0; i < imageResourceID.length; i++) {
            Log.i("imageResources = ",""+i);
            imgeList.add(imageResourceID[i]);
        }

        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(getContext()).load(path).into(imageView);
            }
        });
        banner.setImages(imgeList);
        banner.isAutoPlay(true);
        banner.start();
        return root;
    }
}